tests/installed: Prefer python3 over python2
authorJonathan Lebon <jonathan@jlebon.com>
Thu, 19 Apr 2018 13:58:49 +0000 (09:58 -0400)
committerAtomic Bot <atomic-devel@projectatomic.io>
Mon, 23 Apr 2018 18:33:45 +0000 (18:33 +0000)
In a world progressively unapproving of python2, let's be a bit smarter
and support testing on platforms that only have python3 installed.

Closes: #1546
Approved by: cgwalters

tests/installed/libinsttest.sh

index 4968adc0e374cdea521e23b2ada8e6dfc3426752..1c192b8e92f42f3d33e4cbb4664cbffe2c1e3c11 100644 (file)
@@ -37,6 +37,16 @@ prepare_tmpdir() {
     cd ${test_tmpdir}
 }
 
+if test -x /usr/bin/python3; then
+    export PYTHON=/usr/bin/python3
+    export PYTHONHTTPSERVER=http.server
+elif test -x /usr/bin/python; then
+    export PYTHON=/usr/bin/python
+    export PYTHONHTTPSERVER=SimpleHTTPServer
+else
+    fatal "no python found"
+fi
+
 # This is copied from flatpak/flatpak/tests/test-webserver.sh
 run_tmp_webserver() {
     dir=$1
@@ -44,22 +54,22 @@ run_tmp_webserver() {
     test -n ${test_tmpdir}
 
     cd ${dir}
-    env PYTHONUNBUFFERED=1 setsid python -m SimpleHTTPServer 0 &>${test_tmpdir}/httpd-output &
+    env PYTHONUNBUFFERED=1 setsid $PYTHON -m $PYTHONHTTPSERVER 0 &>${test_tmpdir}/httpd-output &
     cd -
     child_pid=$!
 
-    for x in $(seq 50); do
+    for x in $(seq 10); do
         # Snapshot the output
         cp ${test_tmpdir}/httpd-output{,.tmp}
         # If it's non-empty, see whether it matches our regexp
-        if test -s ${test_tmpdir}/httpd-output.tmp; then
-            sed -e 's,Serving HTTP on 0.0.0.0 port \([0-9]*\) \.\.\.,\1,' < ${test_tmpdir}/httpd-output.tmp > ${test_tmpdir}/httpd-port
+        if test -s ${test_tmpdir}/httpd-output.tmp; then       # py3's http.server prints the http:// address also
+          sed -e 's,Serving HTTP on 0.0.0.0 port \([0-9]*\)\( (http://[^)]*)\)\? \.\.\.,\1,' < ${test_tmpdir}/httpd-output.tmp > ${test_tmpdir}/httpd-port
             if ! cmp ${test_tmpdir}/httpd-output.tmp ${test_tmpdir}/httpd-port 1>/dev/null; then
                 # If so, we've successfully extracted the port
                 break
             fi
         fi
-        sleep 0.1
+        sleep 1
     done
     port=$(cat ${test_tmpdir}/httpd-port)
     echo "http://127.0.0.1:${port}" > ${test_tmpdir}/httpd-address
@@ -75,16 +85,6 @@ fi
 # We need to be root
 assert_streq $(id -u) 0
 
-PYTHON=
-for py in /usr/bin/python3 /usr/bin/python; do
-    if ! test -x ${py}; then continue; fi
-    export PYTHON=${py}
-    break
-done
-if test -z "${PYTHON}"; then
-    fatal "no python found"
-fi
-
 rpmostree_query_json() {
     query=$1
     rpm-ostree status --json | $PYTHON -c 'import json,sys; v=json.load(sys.stdin); print(v'${query}')'